package com.jyuxuan.recruitment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jyuxuan.recruitment.mapper.DemandMapper;
import com.jyuxuan.recruitment.mapper.EnterpriseMapper;
import com.jyuxuan.recruitment.pojo.Demand;
import com.jyuxuan.recruitment.pojo.Enterprise;
import com.jyuxuan.recruitment.service.DemandService;
import com.jyuxuan.recruitment.utils.DataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DemandServiceImpl implements DemandService {

    @Autowired
    private DemandMapper demandMapper;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    /**
     * 企业查询用人需求
     *
     * @param number
     * @return
     */
    public DataMap demandsView(String number) {
        Enterprise enterprise = enterpriseMapper.findByNumber(number);
        int eId = enterprise.getEnterpriseId();
        List<Demand> demands = demandMapper.findByEid(eId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("demands", demands);
        return DataMap.success().setData(jsonObject);
    }

    /**
     * 管理员查询用人需求
     *
     * @return
     */
    public DataMap demandsAllView(Demand demand) {
        List<Demand> demands = demandMapper.findDynamic(demand);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("demands", demands);
        return DataMap.success().setData(jsonObject);
    }


    /**
     * 管理员审核用人需求
     *
     * @param demand
     * @return
     */
    public DataMap demandAudit(Demand demand) {
        int state = demand.getState();
        Demand demand1 = demandMapper.findById(demand.getDemandId());
        demand1.setState(state);
        int n = demandMapper.update(demand1);
        if (n > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }


    /**
     * 提交用人需求
     *
     * @param demand
     * @param number
     * @return
     */
    public DataMap demandInsert(Demand demand, String number) {
        Enterprise enterprise = enterpriseMapper.findByNumber(number);
        demand.setEname(enterprise.getEname());
        demand.setEnterpriseId(enterprise.getEnterpriseId());
        int n = demandMapper.save(demand);
        if (n > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }

    /**
     * 修改用人需求
     *
     * @param demand
     * @return
     */
    public DataMap demandModify(Demand demand) {
        int n = demandMapper.update(demand);
        if (n > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }

    /**
     * 删除用人需求
     *
     * @param demand
     * @return
     */
    public DataMap demandDelete(Demand demand) {
        int n = demandMapper.delete(demand);
        if (n > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }

}
